package com.gxa.travelphone.config;

import com.cloopen.rest.sdk.BodyType;
import com.gxa.travelphone.exception.SystemException;
import entity.Member;
import lombok.Data;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Data
@Component
@Slf4j
public class SmsConfig {

    private String accountSID = "8aaf0708780055cd017839effdcf14b9";

    private String accountToken = "4159fb9cdddd4249a2d4d737fc3f3749";

    private String appId = "8aaf0708780055cd017839effed514bf";

    private String templateId = "1";

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource(name = "objectRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate01;
    //生产环境请求地址
    private static final String serverIp = "app.cloopen.com";
    //请求端口
    private static final String serverPort = "8883";
    //存入redis的前缀
    private static final String KEY_PREFIX = "smsCode:";
    //存活时间
    private static final Long SMS_MIN_INTERVAL_IN_MILLIS = 60000L;

    public void sendSms(String phone){
        String code = RandomStringUtils.random(4,"0123456789qwertyuiopasdfghjklzxcvbnm");
        String[] data = {code,"2"};
        String redisKey = KEY_PREFIX + phone;
//        //按照手机号限流，先从redis中取值
//        String redisValue = redisTemplate.opsForValue().get(redisKey);
//        if (StringUtils.isNoneBlank(redisValue)) {
//            //redis中有值
//            Long aLong = Long.valueOf(redisValue);
//            if (System.currentTimeMillis()-aLong<SMS_MIN_INTERVAL_IN_MILLIS){
//                throw  new SystemException(1001,"发送频繁！请稍后再试！");
//            }
//        }
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp,serverPort);
        sdk.setAccount(accountSID,accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        //发送短信至手机号
        HashMap<String, Object> sendTemplateSMS = sdk.sendTemplateSMS(phone, templateId, data);
        if ("000000".equals(sendTemplateSMS.get("statusCode"))){
            HashMap<String,Object> data01 = (HashMap<String, Object>) sendTemplateSMS.get("data");
            Set<String> keySet = data01.keySet();
            for (String key : keySet) {
                Object o = data01.get(key);
            }
            //发送成功后写入redis
            redisTemplate01.opsForSet().add(redisKey,code);
            redisTemplate01.expire(redisKey, 120, TimeUnit.SECONDS);
        }else {
            throw new SystemException(1002,"错误码=" + sendTemplateSMS.get("statusCode") +" 错误信息= "+sendTemplateSMS.get("statusMsg"));
        }
    }

}
